package cn.dgut.training.service.impl;

import cn.dgut.training.controller.TransactionService;
import cn.dgut.training.dto.GroupBuyingDTO;
import cn.dgut.training.dto.GroupBuyingRulesDTO;
import cn.dgut.training.emun.GroupEnoughEnum;
import cn.dgut.training.emun.GroupRulesStatusEnum;
import cn.dgut.training.emun.GroupStatusEnum;
import cn.dgut.training.entity.activity.GroupBuying;
import cn.dgut.training.entity.activity.GroupBuyingRules;
import cn.dgut.training.entity.transaction.GroupJoin;
import cn.dgut.training.mapper.GroupBuyingMapper;
import cn.dgut.training.mapper.GroupBuyingRulesMapper;
import cn.dgut.training.service.IGroupBuyingRulesService;
import cn.dgut.training.service.IGroupBuyingService;
import cn.dgut.training.utils.RestUtil;
import cn.dgut.training.vo.CommonResponse;
import cn.dgut.training.vo.GroupBuyingRulesVO;
import cn.dgut.training.vo.GroupBuyingVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.internal.$Gson$Preconditions;
import com.google.gson.internal.LinkedTreeMap;
import org.apache.catalina.Group;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.validation.GroupSequence;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@Service
public class GroupBuyingServiceImpl extends ServiceImpl<GroupBuyingMapper, GroupBuying> implements IGroupBuyingService {

    @Resource
    private GroupBuyingMapper groupBuyingMapper;

    @Resource
    private GroupBuyingRulesMapper groupBuyingRulesMapper;

    @Autowired
    private TransactionService transactionService;

    @Override
    public GroupBuyingVO createActivity(@RequestBody @Valid GroupBuyingDTO groupBuyingDTO) {
        GroupBuying groupBuying = new GroupBuying();
        BeanUtils.copyProperties(groupBuyingDTO,groupBuying);

        long counts = groupBuyingDTO.getCount();
        long rid = groupBuyingDTO.getRulesId();

        GroupBuyingRules Rules = groupBuyingRulesMapper.selectById(rid);
        groupBuying.setGoodsId(Rules.getGoodsId());
        groupBuying.setGoodsName(Rules.getGoodsName());
        groupBuying.setPicUrl(Rules.getPicUrl());
        groupBuying.setDiscount(Rules.getDiscount());
        groupBuying.setDiscountMember(Rules.getDiscountMember());
        groupBuying.setExpireTime(Rules.getExpireTime());
        groupBuying.setProductId(Rules.getProductId());
        groupBuying.setProductNumber(Rules.getProductNumber());
        groupBuying.setSpecifications(Rules.getSpecifications());

        if(Rules.getStatus() == GroupRulesStatusEnum.RULES_FOR_EXPIRELINE.getValue()){
            groupBuying.setStatus(GroupStatusEnum.ACTIVITY_FOR_EXPIRELINE.getValue());
        }else if(Rules.getStatus() == GroupRulesStatusEnum.RULES_FOR_OFFLINE.getValue()){
            groupBuying.setStatus(GroupStatusEnum.ACTIVITY_FOR_OFFLINE.getValue());
        }else if(Rules.getStatus() == GroupRulesStatusEnum.RULES_FOR_ONLINE.getValue()){
            groupBuying.setStatus(GroupStatusEnum.ACTIVITY_FOR_ONLINE.getValue());
        }


        for(int i=0;i<counts;i++){
            groupBuyingMapper.insert(groupBuying);
        }

        return null;
    }

    @Override
    public Integer updateActivity(Long id) {
        GroupBuying groupBuying = groupBuyingMapper.selectById(id);
        Integer boundarym = groupBuying.getBoudaryMember()+1;
        Integer item=new Integer(0);
        if(boundarym >=groupBuying.getDiscountMember()){
            groupBuying.setBoudaryMember(boundarym);
            groupBuying.setStatus(GroupStatusEnum.ACTIVITY_FOR_ENOUGH.getValue());
            groupBuyingMapper.updateById(groupBuying);
            item=1;
        }else{
            groupBuying.setBoudaryMember(boundarym);
            groupBuyingMapper.updateById(groupBuying);
        }
        return item;
    }

    @Override
    public GroupBuyingVO listAllActivityByPage(GroupBuyingDTO groupBuyingDTO){
        //构造分页
        IPage ipage = new Page();
        BeanUtils.copyProperties(groupBuyingDTO, ipage);
        //获取条件查询构造器
        QueryWrapper queryWrapper = GroupBuyingDTO.getWrapper(groupBuyingDTO);

        IPage res = groupBuyingMapper.selectPage(ipage, queryWrapper);

        GroupBuyingVO groupBuyingVO = new GroupBuyingVO(res);

        return groupBuyingVO;
    }

    @Override
    public GroupBuyingVO deleteActivity(GroupBuyingDTO groupBuyingDTO) {
        GroupBuying groupBuying = new GroupBuying();
        BeanUtils.copyProperties(groupBuyingDTO,groupBuying);

        groupBuying.setStatus(GroupStatusEnum.ACTIVITY_FOR_OFFLINE.getValue());
        groupBuyingMapper.updateById(groupBuying);

        groupBuyingMapper.deleteById(groupBuying);

        GroupBuyingVO groupBuyingVO = new GroupBuyingVO();
        BeanUtils.copyProperties(groupBuying,groupBuyingVO);

        return groupBuyingVO;
    }

    @Override
    public GroupBuyingVO findUserJoinGroup(Long userId, GroupBuyingDTO groupBuyingDTO) {

        CommonResponse commonResponse = transactionService.findGroupJoinByUserId(userId);
        List<LinkedTreeMap> groupJoinList = (List) RestUtil.getObject(commonResponse, List.class);
        List<GroupJoin> groupJoinList1=new ArrayList<>();
        for(LinkedTreeMap groupJoin:groupJoinList){
            GroupJoin groupJoin1=new GroupJoin();
            Double orderId=(Double) groupJoin.get("orderId");
            Double activityId= (Double) groupJoin.get("activityId");
            Double userId1=(Double) groupJoin.get("userId");
            groupJoin1.setActivityId(activityId.longValue()).setUserId(userId1.longValue()).setOrderId(orderId.longValue());
            groupJoinList1.add(groupJoin1);
            System.out.println(orderId);
        }

        System.out.println(groupJoinList);


        IPage ipage = new Page();
        BeanUtils.copyProperties(groupBuyingDTO, ipage);

        List<GroupBuying> g = new ArrayList<>();

        for(GroupJoin groupJoin:groupJoinList1){
            Long gid = groupJoin.getActivityId();
            GroupBuying groupBuying = groupBuyingMapper.selectById(gid);
            g.add(groupBuying);
        }

        GroupBuyingVO groupBuyingVO = new GroupBuyingVO();
        BeanUtils.copyProperties(ipage,groupBuyingVO);
        groupBuyingVO.setPages(20);
        groupBuyingVO.setList(g);

        return groupBuyingVO;
    }
}
